<?php
namespace app\api\controller;
use think\Db;
use think\Image;
use think\Controller;
use think\Request;
class Deom extends Controller
{
  public function AjaxReturn($code,$msg,$data=array())
  {

    $returnData=array(
      "code"=>$code
    );
    if($msg){
      $returnData["message"]=$msg;
    }
    if(!empty($data))
    {
      $returnData["data"]=$data;
    }
    exit(json_encode($returnData));
  }


  public function chakan($openid){
    $id=db('user')->where('code',$openid)->value('uid');
    return $id;
  }


  //整理粉丝表
  /* public function fans(){
   	$uid=input('uid');
    	$q=db('fans')->where('uid',$uid)->update(['guid'=>'']);//先把粉丝表中的关注清空
        $w=db('fans')->where('uid',$uid)->update(['fuid'=>'']);//先把粉丝表中的粉丝清空
        $b=db('ribiao')->where('uid',$uid)->select();//根据uid去查日志表里的主动关注信息
     	$guid='';
      	for($i=0;$i<count($b);$i++){
        	$guid = $guid.$b[$i]['bid'].',';
        }
      	$sql1=db('fans')->where('uid',$uid)->update(['guid'=>$guid]);
        $b=db('ribiao')->where('bid',$uid)->select();//根据uid去查日志表里的被别人关注信息
      	$fuid='';
      	for($j=0;$j<count($b);$j++){
        	$fuid = $fuid.$b[$j]['uid'].',';
        }
      	$sql2=db('fans')->where('uid',$uid)->update(['fuid'=>$fuid]);
     if($sql1){
     	return $this->AjaxReturn(1,'操作成功',$sql1);
     }else{
     	return $this->AjaxReturn(-1,'操作失败',$sql1);
     }
   }*/


  //日志表添加数据
  /*public function ribiao(){
  	$uid=db('fans')->where('uid','neq',27)->where('uid','neq',46)->where('uid','neq',66)->select();
    for($i=0;$i<count($uid);$i++){
    	if($uid[$i]['fuid'] != ''){
        	$arr=array_filter(explode(',',$uid[$i]['fuid']));
          	for($j=0;$j<count($arr);$j++){
            	$sql=db('ribiao')->insert([
                  					'uid'=>$arr[$j],
                                    'bid'=>$uid[$i]['uid'],
                  					'createtime'=>date('Y-m-d H:i:s',time()),
                  					'zhaungtai'=>1
                                          ]);
            }
        }
    }

  }*/

  //官方账号撤销 别人取消关注
  /* public function delgf(){
  	$ri=db('ribiao')->where('bid',1)->select();
    for($i=0;$i<count($ri);$i++){
    	$guid=db('fans')->where('uid',$ri[$i]['uid'])->find();
      	$guid=array_filter(explode(',',$guid['guid']));
      	$key = array_search(1,$guid);
      	unset($guid[$key]);
      	$guid=implode(',',$guid).',';
      	$a=db('fans')->where('uid',$ri[$i]['uid'])->update(['guid'=>$guid]);
      	$b=db('ribiao')->where('uid',$ri[$i]['uid'])->delete();
    }
  }*/
  public function data(){
    $data=db('message')->where('mided','neq',0)->select();
    foreach($data as $key=>$value){
      $yuan = db('message')->where('mid',$value['mided'])->find();
      db('message')->where('mid',$value['mid'])->update([
        'num' => $yuan['num'],
        'id' => $yuan['id']
      ]);
    }
  }

  //点赞添加的信息
  /*  public function dianzan()
  {
     $openid = $this->uid;//openid
      	$name = db('user')->where('uid',$openid)->find();
      	$name['uname'] = $name['uname']==''? $name['nickname']:$name['uname'];
        $mid = input('mid');//接收文章mid
        $uflg=db('user')->where('uid',$openid)->value('uflg');
      	$mes = new Message();
        if($uflg!=0)
        {
            if(!empty($mid))
            {
                $cha = db('message')->where('mid', $mid)->field('id')->find();
                if ($cha['id'] == '') {
                    $str = $openid . ',';
                    $cha = db('message')->where('mid', $mid)->update(['id' => $str]);
                    $time = date('Y-m-d H:i:s');
                    $rs = db('praise')->insert(['mid' => $mid, 'uid' => $openid, 'createtime' => $time]);
                    if ($cha) {
                       $abc = db('message')->where('mid', $mid)->field('id,uid,content')->find();
                      if($openid != $abc['uid']){
                        $abc['id']=explode (',',$abc['id']);
                        $abc['id']=array_filter($abc['id']);
                        $abc['id']=count($abc['id']);
                        $openid=db('user')->where('uid',$abc['uid'])->field('code')->find();
                        $a = $mes->dianzan($openid['code'],$name['uname'],$abc['uid'],$abc['id'],$abc['content']);
                        }
                        //file_put_contents('a.php',$a);
                        return $this->AjaxReturn("1", "点赞成功！",$name['uname']);
                    }
                } else {
                    $id = array_filter(explode(',', $cha['id']));
                    if (in_array($openid, $id)) {
                        return $this->AjaxReturn("-1", "该文章你已经点赞过！");
                    } else {
                        $str = $cha['id'] . $openid . ',';
                        $cha = db('message')->where('mid', $mid)->update(['id' => $str]);
                        $time = date('Y-m-d H:i:s');
                        $rs = db('praise')->insert(['mid' => $mid, 'uid' => $openid, 'createtime' => $time]);
                        if ($cha) {
                            $abc = db('message')->where('mid', $mid)->field('id,uid,title,type,content')->find();
                            $abc['id']=explode (',',$abc['id']);
                            $abc['id']=array_filter($abc['id']);
                            $abc['id']=count($abc['id']);
                            $openid=db('user')->where('uid',$abc['uid'])->field('code')->find();
                          	if($abc['type']==1){
                            	$abc['title']=$abc['content'];
                            }
                            $a = $mes->dianzan($openid['code'],$name['uname'],$abc['uid'],$abc['id'],$abc['title']);
                            //file_put_contents('a.php',$a);
                            return $this->AjaxReturn("1", "点赞成功！",$name['uname']);
                        }
                    }
                }
            }
            else
            {
                return $this->AjaxReturn("-1","未接收到文章mid！");
            }
        }
        else
        {
            return $this->AjaxReturn("-10","黑名单状态无权操作！");
        }

  }*/


  public function InformationList(){
    $page=input('page');
    //获取当前域名
    $request = Request::instance();
    $domain=$request->domain();
    $sql=db('zixun')
      ->where('flg',0)
      ->order('createtime desc')
      ->page($page,6)
      ->select();
    foreach ($sql as $key=>$value){
      $value['prctures']=$domain .$value['picture'];
      $arr[]=$value;
    }
    if (!empty($sql)){
      return $this->AjaxReturn("-1","该用户未实名认证",$arr);
    }else{
      return $this->AjaxReturn("-2","操作失败");
    }
  }
  //商圈的显示页面
  public function index()
  {
    $openid=input('openid');
    $data = Db::name('user')->where('code',$openid)->find();
    $uid=$data['uid'];
    $page = input('page');
    $attention = Db::name("fans")->where("f_fans.uid",$uid)->field('guid')->find();
    $str=$attention['guid'].$uid;

    $where['me.uid']=array("in",$str);
    $where['me.mflg']=1;

    $ziji['me.uid']=$uid;
    $ziji['me.mflg']=1;

    //未关注别人的时候查询我有没有发布文章
    $list = Db::name("message")
      ->alias('me')
      ->join('f_real re','re.uid=me.uid')
      ->join('f_user us','us.uid=re.uid')
      ->where($ziji)
      ->select();
    //获取当前域名
    $request = Request::instance();
    $domain=$request->domain();
    if(!empty($attention['guid']))
    {
      $list = Db::name("message")
        ->alias('me')
        ->join('f_real re','re.uid=me.uid')
        ->join('f_user us','us.uid=re.uid')
        ->where($where)
        //->whereOr($ziji)
        ->order("me.createtime desc")
        ->page($page,7)
        ->field('me.mid,me.type,me.price,us.uid,me.number,me.unit,me.address,us.uname,us.nickname,me.createtime,me.picture,me.title,re.rflg,me.content,me.mflg,me.num,me.id,us.avatars_img,me.choice,us.head_portrait,me.usered,me.mided')
        ->select();

      /* $list=array();

      for($i=1;$i<=$page;$i++){
        $info=$listed->page($page,7)->select();
        for($a=0;$a<count($info);$a++){
          array_push($list,$info[$a]);
        }
      }*/

      //地址
      $url=$domain."/upload/message/";
      foreach ($list as $key=>$value)
      {	
        //头像
        if($value['avatars_img'])
        {
          $list[$key]['avatars_img']=$domain.'/upload/portrait/'.$value['avatars_img'];
        }else
        {
          if($value['head_portrait']=='')
          {
            $list[$key]['avatars_img']=$domain.'/upload/linshiimg/20190411095739.jpg';
          }else{
            $list[$key]['avatars_img']=$value['head_portrait'];
          }
        }
        //昵称
        if($value['rflg']== 1)
        {
          $list[$key]['nickname']=$list[$key]['uname'];
        }
        //如果是转载文章则显示原文章的数据，不是转载文章显示自己的数据
        if($value['uid'] != $value['usered']){
          $rflg = db('real')->where('uid',$value['usered'])->value('rflg');
          $name = db('user')->where('uid',$value['usered'])->field('nickname,uname')->find();
          $numId=db('message')->where('mid',$value['mided'])->field('num,id')->find();
          $num=$numId['num']+1;
          $numed=db('message')->where('mid',$value['mided'])->update(['num'=>$num]);
          $list[$key]['num']=$num;
          $list[$key]['time'] = db('message')->where('mid',$value['mided'])->value('createtime');
          if($rflg == 1){
            $list[$key]['usered'] = $name['uname'];
          }else{
            $list[$key]['usered'] = $name['nickname'];
          }      
          //点赞数
          $id = array_filter(explode(',',$numId['id']));
          $list[$key]['id']=count($id);
          //判断我有没有点赞
          $list[$key]['dian'] = in_array($uid,$id) ? 1 : 0;
          //评论数
          $list[$key]['pinglun']=db('evalute')->where('message_id',$value['mided'])->where('parent_evalute_id',0)->count();
          $list[$key]['mid'] = $value['mided'];
        }else{
          $list[$key]['usered'] = $list[$key]['nickname'];
          $list[$key]['time'] = $list[$key]['createtime'];
          //浏览量加1
          $list[$key]['num'] = $value['num'] + 1;
          $readNum=db('message')->where('mid',$value['mid'])->update(['num'=>$list[$key]['num']]);
          //点赞数
          $id = array_filter(explode(',',$value['id']));
          $list[$key]['id']=count($id);
          //判断我有没有点赞
          $list[$key]['dian'] = in_array($uid,$id) ? 1 : 0;
          //评论数
          $list[$key]['pinglun']=db('evalute')->where('message_id',$value['mid'])->where('parent_evalute_id',0)->count();
        }
        //单位
        if($value['unit']=="1")
        {
          $list[$key]['unit']='吨';
        }
        elseif($value['unit']=="2")
        {
          $list[$key]['unit']='公斤';
        }
        elseif($value['unit']=="3")
        {
          $list[$key]['unit']='斤';
        }
        else
        {
          $list[$key]['unit']='个';
        }

        //图片
        $a=$list[$key]['picture'];
        $arr=array_filter(explode(',',$a));
        //价格
        if($value['price'] == '商议'){
          $list[$key]['price'] = '';
        }
        //数组非空设置追加路径
        if(!empty($arr))
        {
          for($i=0;$i<count($arr);$i++){
            $arr[$i]=$url.$arr[$i];
          }
          if(count($arr)>3){
            $list[$key]['picture']=array($arr[0],$arr[1],$arr[2]);
          }else{
            $list[$key]['picture']=$arr;
          }
        }
        else
        {
          $list[$key]['picture']=array($url.'no.jpg');
        }
      }
      return $this->AjaxReturn("1",'123',$list);
    }
    elseif(count($list)>0)
    {
      $list = Db::name("message")
        ->alias('me')
        ->join('f_real re','re.uid=me.uid')
        ->join('f_user us','us.uid=re.uid')
        ->where($where)
        //->whereOr($ziji)
        ->order("me.createtime desc")
        ->page($page,7)
        ->field('me.mid,me.type,me.price,us.uid,me.number,me.unit,me.address,us.uname,us.nickname,me.createtime,me.picture,me.title,re.rflg,me.content,me.mflg,me.num,me.id,us.avatars_img,me.choice,us.head_portrait,me.usered,me.mided')
        ->select();

      /*   $list=array();

      for($i=1;$i<=$page;$i++){
        $info=$listed->page($page,7)->select();
        for($a=0;$a<count($info);$a++){
          array_push($list,$info[$a]);
        }
      }*/
      //地址
      $url=$domain."/upload/message/";
      foreach ($list as $key=>$value)
      {
        //头像
        if($value['avatars_img'])
        {
          $list[$key]['avatars_img']=$domain.'/upload/portrait/'.$value['avatars_img'];
        }else
        {
          if($value['head_portrait']=='')
          {
            $list[$key]['avatars_img']=$domain.'/upload/linshiimg/20190411095739.jpg';
          }else{
            $list[$key]['avatars_img']=$value['head_portrait'];
          }
        }
        //昵称
        if($value['rflg']== 1)
        {
          $list[$key]['nickname']=$list[$key]['uname'];
        }

        //如果是转载文章则显示原文章的数据，不是转载文章显示自己的数据
        if($value['uid'] != $value['usered']){
          $rflg = db('real')->where('uid',$value['usered'])->value('rflg');
          $name = db('user')->where('uid',$value['usered'])->field('nickname,uname')->find();
          $num=db('message')->where('mid',$value['mided'])->value('num');
          $num=$num+1;
          $numed=db('message')->where('mid',$value['mided'])->update(['num'=>$num]);
          $list[$key]['num']=$num;
          $list[$key]['time'] = db('message')->where('mid',$value['mided'])->value('createtime');
          $list[$key]['mid'] = $value['mided'];
          if($rflg == 1){
            $list[$key]['usered'] = $name['uname'];
          }else{
            $list[$key]['usered'] = $name['nickname'];
          }
        }else{
          $list[$key]['usered'] = $list[$key]['nickname'];
          $list[$key]['time'] = $list[$key]['createtime'];
          //浏览量加1
          $list[$key]['num'] = $value['num'] + 1;
          $readNum=db('message')->where('mid',$value['mid'])->update(['num'=>$list[$key]['num']]);
        }

        //点赞数
        $id = array_filter(explode(',',$value['id']));
        $list[$key]['id']=count($id);
        //判断我有没有点赞
        $list[$key]['dian'] = in_array($uid,$id) ? 1 : 0;
        //评论数
        $list[$key]['pinglun']=db('evalute')->where('message_id',$value['mid'])->where('parent_evalute_id',0)->count();
        //单位
        if($value['unit']=="1")
        {
          $list[$key]['unit']='吨';
        }
        elseif($value['unit']=="2")
        {
          $list[$key]['unit']='斤';
        }
        elseif($value['unit']=="3")
        {
          $list[$key]['unit']='斤';
        }
        else
        {
          $list[$key]['unit']='个';
        }
        //价格
        if($value['price'] == '商议'){
          $list[$key]['price'] = '';
        }
        //图片
        $a=$list[$key]['picture'];
        $arr=array_filter(explode(',',$a));
        //数组非空设置追加路径
        if(!empty($arr))
        {
          for($i=0;$i<count($arr);$i++){
            $arr[$i]=$url.$arr[$i];
          }
          if(count($arr)>3){
            $list[$key]['picture']=array($arr[0],$arr[1],$arr[2]);
          }else{
            $list[$key]['picture']=$arr;
          }
        }
        else
        {
          $list[$key]['picture']=array($url.'no.jpg');
        }
      }
      return $this->AjaxReturn("2","456",$list);
    }
    else
    {
      return $this->AjaxReturn("-1","123",$list);
    }
  }

  //发现页面刷新总页数数据
  public function faxianPage()
  {
    $openid=$this->chakan(input('openid'));
    //信息流中出去发现的信息
    $page=input('page');
    $where ['mflg'] = 1;
    $where['faxian']=1;

    $rs2=db("message")
      ->alias('me')
      ->join('f_user','me.uid=f_user.uid','left')
      ->join('f_real','f_user.uid=f_real.uid','left')
      ->where($where)
      //->where('me.uid','eq','me.usered')
      ->field('me.type,me.picture,f_user.uid,me.address,me.mid,me.title,me.number,me.unit,f_user.nickname,f_user.uname,me.price,f_real.rflg,content,f_message.createtime,f_message.num,f_message.id,f_user.avatars_img,f_user.head_portrait,me.choice,me.usered')
      ->order('me.state=1 DESC,me.paixu DESC')
      ->select();
    //信息流中出去发现的信息
    $where ['mflg'] = 1;
    $where['faxian']=0;
    $rs1=db("message")
      ->alias('me')
      ->join('f_user','me.uid=f_user.uid','left')
      ->join('f_real','f_user.uid=f_real.uid','left')
      ->where($where)
      // ->where('me.uid','eq','me.usered')
      ->field('me.type,me.picture,f_user.uid,me.address,me.mid,me.title,me.number,me.unit,f_user.nickname,f_user.uname,me.price,f_real.rflg,content,f_message.createtime,f_message.num,f_message.id,f_user.avatars_img,f_user.head_portrait,me.choice,me.usered')
      ->order('me.createtime DESC')
      ->select();
    $rs=array();

    for($i=1;$i<=$page;$i++){
      $info=array_merge($rs2, $rs1);
      for($j=0;$j<count($info);$j++){
        if($info[$j]['uid'] != $info[$j]['usered']){
          unset($info[$j]);
        }
      }
      $info=array_values($info);
      $info=arrayPage($info,7,$i);
      for($a=0;$a<count($info);$a++){
        array_push($rs,$info[$a]);
      }
    }
    //获取当前域名
    $request = Request::instance();
    $domain=$request->domain();
    $url=$domain."/upload/message/";

    foreach ($rs as $key=>$value)
    {  
      //删除转载文章的信息
      //if($value['uid']!=$value['usered']){
      //$rs=array_splice($rs,$key,1);
      // dump($rs);
      // continue;
      //   }
      //我是否关注此用户
      $guan=explode(',',db('fans')->where('uid',$value['uid'])->value('fuid'));
      if(!in_array($openid,$guan)){
        $rs[$key]['fans'] = 0;
      }
      //头像
      if($value['avatars_img'])
      {
        $rs[$key]['avatars_img'] = $domain.'/upload/portrait/'. $value['avatars_img'];
      }
      else
      {
        if($value['head_portrait']=='')
        {
          $rs[$key]['avatars_img']=$domain.'/upload/linshiimg/20190411095739.jpg';
        }else{
          $rs[$key]['avatars_img']=$value['head_portrait'];
        }
      }
      //浏览量加1
      $rs[$key]['num'] = $value['num'] + 1;
      $readNum=db('message')->where('mid',$value['mid'])->update(['num'=>$rs[$key]['num']]);
      //昵称
      if($value['rflg']=="1"){
        $rs[$key]['nickname']=$value['uname'];
        //文章发布人
        $rs[$key]['usered']=$value['uname'];
      }else{
        $rs[$key]['usered']=$value['nickname'];
      }
      //文章时间
      $rs[$key]['time']=$value['createtime'];
      //单位
      if($value['unit']=="1")
      {
        $rs[$key]['unit']='吨';
      }
      if($value['unit']=="2")
      {
        $rs[$key]['unit']='公斤';
      }
      if($value['unit']=="3")
      {
        $rs[$key]['unit']='斤';
      }
      if($value['unit']=="4")
      {
        $rs[$key]['unit']='个';
      }
      //我有没有点赞此文章
      $id=array_filter(explode(',',$value['id']));
      if(in_array($openid,$id)){
        $rs[$key]['dian'] = 1;
      }else{
        $rs[$key]['dian'] = 0;
      }
      //点赞数
      $rs[$key]['id']=count($id);
      //评论数
      $rs[$key]['pinglun']=db('evalute')->where('message_id',$value['mid'])->where('parent_evalute_id',0)->count();
      //价格
      if($value['price'] == '商议'){
        $rs[$key]['price'] = '';
      }
      //图片
      $a=$value['picture'];
      $arr=array_filter(explode(',',$a));
      if(empty($arr)){
        $rs[$key]['picture']=array($url."no.jpg");
      }else{
        //$absolute= "/upload/suo/"; 

        for($i=0;$i<count($arr);$i++){
          // if (!is_dir(ROOT_PATH."/upload/suo/".$arr[$i]))
          // {
          // $image = Image::open(ROOT_PATH."/upload/message/".$arr[$i]);
          //    $pic = $image->thumb(750,1000,Image:: THUMB_SCALING)->save(ROOT_PATH.$absolute.$arr[$i]);
          //   }
          $arr[$i]=$url.$arr[$i];
        }
        if(count($arr)>3){
          $rs[$key]['picture']=array($arr[0],$arr[1],$arr[2]);
        }else{
          $rs[$key]['picture']=$arr;
        }
      }
    }
    if($rs){
      return  $this->AjaxReturn("1","操作成功",$rs);
    }else{
      return  $this->AjaxReturn("-1","暂无信息");
    }
  }
  //找产品搜索
  public function chanSearch(){
    $openid=$this->chakan(input('openid'));
    //获取当前域名
    $request = Request::instance();
    $domain=$request->domain();
    if(input('title')!=""){
      $page=input('page');
      $where['mflg'] = 1;
      $where['type']=1;
      //$where ['f_message.stoptime'] = array("egt",date("Y-m-d H:i:s"));
      $condition=1;
      $condition .= " and content like '%".input('title')."%' or choice  like '%".input('title')."%' or price  like '%".input('title')."%'or unit  like '%".input('title')."%' or number  like '%".input('title')."%'or address  like '%".input('title')."%'  ";
      $rs=db("message")
        ->alias('me')
        ->join('f_user us','me.uid=us.uid','left')
        ->join('f_real re','us.uid=re.uid','left')
        ->where($where)
        ->where($condition)
        /*->where('content','like','%'.input('title').'%')
            ->whereor('choice','like','%'.input('title').'%')
            ->whereor('price','like','%'.input('title').'%')
          	->whereor('number','like','%'.input('title').'%')
            ->whereor('address','like','%'.input('title').'%')
            ->whereor('unit','like','%'.input('title').'%')*/
        ->field('me.mid,me.type,me.price,us.uid,me.number,me.unit,me.address,us.uname,me.createtime,nickname,me.picture,me.title,re.rflg,me.content,me.num,me.id,us.avatars_img,f_user.head_portrait,me.choice')
        ->order("me.state=1 desc,me.faxian=1 desc,me.createtime DESC")
        ->page($page,'7')
        ->select();
      $url="https://www.agrimanu.cn/upload/message/";
      foreach ($rs as $key=>$value)
      {
        //我是否关注此用户
        $guan=explode(',',db('fans')->where('uid',$value['uid'])->value('fuid'));
        if(!in_array($openid,$guan)){
          $rs[$key]['fans'] = 0;
        }
        //浏览量加1
        $rs[$key]['num'] = $value['num'] + 1;
        $readNum=db('message')->where('mid',$value['mid'])->update(['num'=>$rs[$key]['num']]);
        //头像
        if($value['avatars_img'])
        {
          $rs[$key]['avatars_img']=$domain.'/upload/portrait/'.$value['avatars_img'];
        }else
        {
          if($value['head_portrait']=='')
          {
            $rs[$key]['avatars_img']=$domain.'/upload/linshiimg/20190411095739.jpg';
          }else{
            $rs[$key]['avatars_img']=$value['head_portrait'];
          }
        }
        //昵称
        if($value['rflg']=="1"){
          $rs[$key]['nickname']=$rs[$key]['uname'];
        }
        //单位
        if($value['unit']=="1")
        {
          $rs[$key]['unit']='吨';
        }
        if($value['unit']=="2")
        {
          $rs[$key]['unit']='公斤';
        }
        if($value['unit']=="3")
        {
          $rs[$key]['unit']='斤';
        }
        if($value['unit']=="4")
        {
          $rs[$key]['unit']='个';
        }
        //我有没有点赞此文章
        $id=array_filter(explode(',',$value['id']));
        if(in_array($openid,$id)){
          $rs[$key]['dian'] = 1;
        }else{
          $rs[$key]['dian'] = 0;
        }
        //点赞数
        $rs[$key]['id']=count($id);
        //评论数
        $rs[$key]['pinglun']=db('evalute')->where('message_id',$rs[$key]['mid'])->where('parent_evalute_id',0)->count();
        //价格
        if($value['price'] == '商议'){
          $rs[$key]['price'] = '';
        }
        //图片
        $a=$rs[$key]['picture'];
        $arr=array_filter(explode(',',$a));
        if(empty($arr)){
          $rs[$key]['picture']=array($url."no.jpg");
        }else{
          for($i=0;$i<count($arr);$i++){
            $arr[$i]=$url.$arr[$i];
          }
          if(count($arr)>3){
            $rs[$key]['picture']=array($arr[0],$arr[1],$arr[2]);
          }else{
            $rs[$key]['picture']=$arr;
          }
        }
      }
      if($rs){
        return $this->AjaxReturn("1","操作成功",$rs);
      }else{
        return $this->AjaxReturn("-1","暂无信息");
      }
    }else{
      return $this->AjaxReturn('-1','暂无信息');
    }
  }
  public function adds(){
    //$uid=$this->uid;
    $uid=db('user')->where('code',input('openid'))->value('uid');
    //用户是否黑名单
    $sql=db('user')
      ->where("uid",$uid)
      ->field('uflg')
      ->find();
    if ($sql['uflg']==0){
      return $this->AjaxReturn("123","黑名单无权操作");
    }
    //一天之内发布次数是否超过规定次数
    $times=date('Y-m-d 00:00:00');
    $timee=date('Y-m-d H:i:s');
    $where ['createtime'] = array("between",array($times,$timee));
    $where['uid']=$uid;
    $res=db('message')
      ->where($where)
      ->count();
    $k=db('shezhi')
      ->find();
    $rs=db('real')
      ->where("uid",$uid)
      ->field('rid,rflg')
      ->find();
    //用户没有实名认证并且超过普通用户发布次数
    if($rs['rflg']!="1"&&$res>=$k['userwenzhang']){
      return $this->AjaxReturn("12","今日超出发布次数");
    }
    //用户已实名认证并且超过vip用户发布次数
    if($rs['rflg']=="1"&&$res>=$k['vipwenzhang']){
      return $this->AjaxReturn("22","今日超出发布次数");
    }

    $date['content']=input('content');//内容
    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx6c1a86ee3e43e4a3&secret=32f54b415ab46d898b2714c327b8d4b9";
    // 创建一个新cURL资源
    $ch  =  curl_init ();
    // 设置URL和相应的选项
    curl_setopt ( $ch , CURLOPT_URL ,  $url );
    curl_setopt ( $ch , CURLOPT_HEADER ,  false );
    curl_setopt ( $ch , CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ( $ch , CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt ( $ch , CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt ( $ch , CURLOPT_SSLVERSION, 1);
    // 抓取URL并把它传递给浏览器
    $str = curl_exec ( $ch );
    //关闭cURL资源，并且释放系统资源
    curl_close ( $ch );
    //将str->数组
    $arr = json_decode($str , true);
    $url = "https://api.weixin.qq.com/wxa/msg_sec_check?access_token=".$arr['access_token'];
    $content=$date['content'];
    $file_data = '{ "content":"'.$content.'" }';//$content(需要检测的文本内容，最大520KB)
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch , CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch , CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $file_data);
    $output = curl_exec($ch);//发送请求获取结果
    curl_close($ch);//关闭会话
    $output=json_decode($output,true);
    if($output['errmsg']!="ok")
    {
    	return $this->AjaxReturn('0','您填写的文字里含有敏感词汇，请删除后重新发布');
    }
    $date['createtime']=date("Y-m-d H:i:s");//创建时间
    $date['address']=input('address/a');//地址数组
    $date['address'] = implode('',$date['address']);//地址
    $date['uid']=$uid;//用户id
    $date['type']=1;//类型
    $date['number']=input('number');//数量
    $date['price']=input('price');//价格
    $date['unit']=input('unit');//单位
    $date['choice']=input('choose');//供应 & 采购
    $date['usered']=$uid;
    $add=db('message')
      ->insertGetId($date);//添加数据并返回mid用来后续添加图片
    if ($add){
      return $this->AjaxReturn('1','发布成功',$add);
    }else{
      return $this->AjaxReturn('0','发布失败');
    }
  }
  //自己产品信息的显示（已测试）
  public function message()
  {
    $request = Request::instance();
    $domain=$request->domain();
    $code=input('openid');
    $page=input('page');
    $uid=Db::name('user')->where('code',$code)->field('uid')->find();
    $uid=$uid['uid'];
    $time=date('Y-m-d H:i:s',time());
    $user=db('user')->where('uid',$uid)->find();
    $rflg=db('real')->where('uid',$uid)->value('rflg');
    $where['uid']=$uid;
    $where['mflg']=1;
    $xinxi=db('message')
      ->where($where)
      ->order('createtime desc')
      ->page($page,7)
      ->select();
    if($xinxi==[])
    {
      $xin='';
      return $xin;
    }else{
      for($i=0;$i<count($xinxi);$i++)
      {
        $xinxi[$i]['myuid'] = $uid;
        $xinxi[$i]['rflg'] = $rflg;
        if($user['avatars_img'] == '')
        {
          if($user['head_portrait'] == '')
          {
            $xinxi[$i]['avatars_img'] = 'https://www.agrimanu.cn/upload/linshiimg/20190411095739.jpg';
          }else
          {
            $xinxi[$i]['avatars_img'] = $user['head_portrait'];
          }	
        }else{
          $xinxi[$i]['avatars_img'] = $domain.'/upload/portrait/'.$user['avatars_img'];
        }

        //昵称
        if($rflg == 1){
          $xinxi[$i]['nickname'] = $user['uname'];
        }else{
          $xinxi[$i]['nickname'] = $user['nickname'];
        }

        //单位
        if($xinxi[$i]['unit']==1)
        {
          $xinxi[$i]['unit']='吨';
        }
        elseif($xinxi[$i]['unit']==2)
        {
          $xinxi[$i]['unit']='公斤';
        }
        elseif($xinxi[$i]['unit']==3)
        {
          $xinxi[$i]['unit']='斤';
        }elseif($xinxi[$i]['unit']==4)
        {
          $xinxi[$i]['unit']='个';
        }
        $xinxi[$i]['delmid']=$xinxi[$i]['mid'];

        //如果是转载文章则显示原文章的数据，不是转载文章显示自己的数据
        if($xinxi[$i]['uid'] != $xinxi[$i]['usered']){
          $name = db('user')->where('uid',$xinxi[$i]['usered'])->field('nickname,uname')->find();
          $rflged = db('real')->where('uid',$xinxi[$i]['usered'])->value('rflg');
          $numId=db('message')->where('mid',$xinxi[$i]['mided'])->field('num,id')->find();
          $num = $numId['num'] + 1;
          $numed=db('message')->where('mid',$xinxi[$i]['mided'])->update(['num'=>$num]);
          $xinxi[$i]['num']=$num;
          $xinxi[$i]['time'] = db('message')->where('mid',$xinxi[$i]['mided'])->value('createtime');
          if($rflged == 1){
            $xinxi[$i]['usered'] = $name['uname'];
          }else{
            $xinxi[$i]['usered'] = $name['nickname'];
          }
          //点赞数
          $id=array_filter(explode(',',$numId['id']));
          $xinxi[$i]['id']=count($id);
          //我有没有点赞此文章
          if(in_array($uid,$id)){
            $xinxi[$i]['dian'] = 1;
          }else{
            $xinxi[$i]['dian'] = 0;
          }
          //评论数
          $xinxi[$i]['pinglun']=db('evalute')->where('message_id', $xinxi[$i]['mided'])->where('parent_evalute_id',0)->count();
          $xinxi[$i]['mid'] = $xinxi[$i]['mided'];
        }else{
          $xinxi[$i]['usered'] = $xinxi[$i]['nickname'];
          $xinxi[$i]['time'] = $xinxi[$i]['createtime'];
          //浏览量加1
          $xinxi[$i]['num'] =  $xinxi[$i]['num'] + 1;
          $readNum=db('message')->where('mid', $xinxi[$i]['mid'])->update(['num'=> $xinxi[$i]['num']]);
          //点赞数
          $id=array_filter(explode(',',$xinxi[$i]['id']));
          $xinxi[$i]['id']=count($id);
          //我有没有点赞此文章
          if(in_array($uid,$id)){
            $xinxi[$i]['dian'] = 1;
          }else{
            $xinxi[$i]['dian'] = 0;
          }
          //评论数
          $xinxi[$i]['pinglun']=db('evalute')->where('message_id', $xinxi[$i]['mid'])->where('parent_evalute_id',0)->count();
        }
        //价格
        if($xinxi[$i]['price'] == '商议'){
          $xinxi[$i]['price'] = '';
        }
        //图片
        $rs[]=array_filter(explode(',',$xinxi[$i]['picture']));
      }
      //输出每组图片的前三张并为其加上地址
      for($i=0;$i<count($rs);$i++)
      {
        if($rs[$i])
        {
          for($j=0;$j<count($rs[$i]);$j++){
            $rs[$i][$j]='https://www.agrimanu.cn/upload/message/'.$rs[$i][$j];
          }
          if(count($rs[$i]) > 3){
            $xinxi[$i]['picture'] = array($rs[$i][0],$rs[$i][1],$rs[$i][2]);
          }else{
            $xinxi[$i]['picture'] = $rs[$i];
          }
        }else{
          $xinxi[$i]['picture']= array('https://www.agrimanu.cn/upload/message/no.jpg');
        }
      }

      return $this->AjaxReturn('1','操作成功',$xinxi);
    }
  }
  //别人名片产品显示
  public function mp()
  {
    $code=input('openid');
    $uid=Db::name('user')->where('code',$code)->field('uid')->find();
    $uid=$uid['uid'];

    $gid=input('uid');
    $page=input('page');
    //发布人的信息
    $user=db('user')->where('uid',$gid)->find();
    $rflg=db('real')->where('uid',$gid)->value('rflg');
    //获取当前域名
    $request = Request::instance();
    $domain=$request->domain();

    $where['uid'] = $gid;
    $where['mflg'] = 1;
    $xinxi = db('message')
      ->where($where)
      ->order('createtime desc')
      ->page($page,7)
      ->select();

    if($xinxi==[])
    {
      $xin='';
      return $xin;
    }else{
      for($i=0;$i<count($xinxi);$i++)
      {   
        if($user['avatars_img'] == '')
        {
          if($user['head_portrait'] == '')
          {
            $xinxi[$i]['avatars_img'] = 'https://www.agrimanu.cn/upload/linshiimg/20190411095739.jpg';
          }else
          {
            $xinxi[$i]['avatars_img'] = $user['head_portrait'];
          }	
        }else{
          $xinxi[$i]['avatars_img'] = $domain.'/upload/portrait/'.$user['avatars_img'];
        }

        //昵称
        if($rflg == 1){
          $xinxi[$i]['nickname'] = $user['uname'];
        }else{
          $xinxi[$i]['nickname'] = $user['nickname'];
        }
        //如果是转载文章则显示原文章发布人，不是转载文章显示自己的名字
        if($xinxi[$i]['uid'] != $xinxi[$i]['usered']){
          $name = db('user')->where('uid',$xinxi[$i]['usered'])->field('nickname,uname')->find();
          $rflged = db('real')->where('uid',$xinxi[$i]['usered'])->value('rflg');
          $numId=db('message')->where('mid',$xinxi[$i]['mided'])->field('num,id')->find();
          $num = $numId['num'] + 1;
          $numed=db('message')->where('mid',$xinxi[$i]['mided'])->update(['num'=>$num]);
          $xinxi[$i]['num']=$num;
          $xinxi[$i]['time'] = db('message')->where('mid',$xinxi[$i]['mided'])->value('createtime');
          if($rflged == 1){
            $xinxi[$i]['usered'] = $name['uname'];
          }else{
            $xinxi[$i]['usered'] = $name['nickname'];
          }
          //我有没有点赞此文章
          $id=array_filter(explode(',',$numId['id']));
          if(in_array($uid,$id)){
            $xinxi[$i]['dian'] = 1;
          }else{
            $xinxi[$i]['dian'] = 0;
          }
          //点赞数
          $xinxi[$i]['id']=count($id);
          //评论数
          $xinxi[$i]['pinglun']=db('evalute')->where('message_id', $xinxi[$i]['mided'])->where('parent_evalute_id',0)->count();
          $xinxi[$i]['mid'] = $xinxi[$i]['mided'];

        }else{
          $xinxi[$i]['usered'] = $xinxi[$i]['nickname'];
          $xinxi[$i]['time'] = $xinxi[$i]['createtime'];
          //浏览量加1
          $xinxi[$i]['num'] =  $xinxi[$i]['num'] + 1;
          $readNum=db('message')->where('mid', $xinxi[$i]['mid'])->update(['num'=> $xinxi[$i]['num']]);
          //我有没有点赞此文章
          $id=array_filter(explode(',',$xinxi[$i]['id']));
          if(in_array($uid,$id)){
            $xinxi[$i]['dian'] = 1;
          }else{
            $xinxi[$i]['dian'] = 0;
          }
          //点赞数
          $xinxi[$i]['id']=count($id);
          //评论数
          $xinxi[$i]['pinglun']=db('evalute')->where('message_id', $xinxi[$i]['mid'])->where('parent_evalute_id',0)->count();
        }
        //我的uid(前台判断是否显示删除按钮)
        $xinxi[$i]['myuid'] = $uid;
        //单位
        if($xinxi[$i]['unit']==1)
        {
          $xinxi[$i]['unit']='吨';
        }
        elseif($xinxi[$i]['unit']==2)
        {
          $xinxi[$i]['unit']='公斤';
        }
        elseif($xinxi[$i]['unit']==3)
        {
          $xinxi[$i]['unit']='斤';
        }elseif($xinxi[$i]['unit']==4)
        {
          $xinxi[$i]['unit']='个';
        }

        //价格
        if( $xinxi[$i]['price'] == '商议'){
          $xinxi[$i]['price'] = '';
        }
        //图片
        $rs[]=array_filter(explode(',',$xinxi[$i]['picture']));
      }
      //输出每组图片的前三张并为其加上地址
      for($i=0;$i<count($rs);$i++)
      {
        if($rs[$i])
        {
          for($j=0;$j<count($rs[$i]);$j++){
            $rs[$i][$j]='https://www.agrimanu.cn/upload/message/'.$rs[$i][$j];
          }
          if(count($rs[$i]) > 3){
            $xinxi[$i]['picture'] = array($rs[$i][0],$rs[$i][1],$rs[$i][2]);
          }else{
            $xinxi[$i]['picture'] = $rs[$i];
          }
        }else{
          $xinxi[$i]['picture']= array('https://www.agrimanu.cn/upload/message/no.jpg');
        }
      }
      return $this->AjaxReturn('1','操作成功',$xinxi);
    }
  }
  //如果是转载文章，则先查看原文章是否删除
  public function see(){
    $mid=input('mid');
    $mflg=db('message')->where('mid',$mid)->find();
    if($mflg['mflg'] == 1){
      return $this->AjaxReturn('1','操作成功');
    }else{
      return $this->AjaxReturn('2','原文已删除');
    }
  }
  //详细页面的查看
  public function ck()
  {
    $openid=input('openid');
    $data = Db::name('user')->where('code',$openid)->find();
    $openid=$data['uid'];
    $mid=input('mid');//接收文章mid
    $uid=input('uid');//接收文章发布人地uid
    //获取当前域名
    $request = Request::instance();
    $domain=$request->domain();
    if(!empty($mid))
    {
      $list = Db::name("message")
        ->alias('me')
        ->where('mid',$mid)
        ->join('f_real re','re.uid=me.uid','left')
        ->join('f_user us','us.uid=re.uid','left')
        ->field('me.mid,me.price,me.number,me.unit,me.address,us.uname,us.nickname,me.createtime,me.picture,me.content,us.company,us.phone,me.num,me.id,me.uid,me.title,re.rflg,us.head_portrait,us.avatars_img,me.choice')
        ->find();
      //头像
      if($list['avatars_img']){
        $list['avatars_img']=$domain.'/upload/portrait/'.$list['avatars_img'];
      }else{
        if($list['head_portrait']=='')
        {
          $list['avatars_img']=$domain.'/upload/linshiimg/20190411095739.jpg';
        }else{
          $list['avatars_img']=$list['head_portrait'];
        }
      }
      //昵称
      if($list['rflg']==1)
      {
        $list['nickname']=$list['uname'];
      }
      //价格
      if($list['price'] == '商议'){
        $list['price'] = '';
      }
      //单位
      if($list['unit']==1)
      {
        $list['unit']='吨';
      }
      elseif($list['unit']==2)
      {
        $list['unit']='公斤';
      }
      elseif($list['unit']==3)
      {
        $list['unit']='斤';
      }
      else
      {
        $list['unit']='个';
      }

      //图片加空
      $url=$domain.'/upload/message/';
      if($list['picture']!='')
      {
        $img=array_filter(explode(',',$list['picture']));
        foreach ($img as $key=>$value9)
        {
          $lis[]=$url.$value9;
        }
        $list['picture']=$lis;
      }
      else
      {
        $list['picture']=array($url.'no.jpg');
      }
      //查询是否关注追加到fans字段
      $fen=db('fans')->where('uid',$uid)->find();
      $arr=array_filter(explode(',',$fen['fuid']));
      if(in_array($openid,$arr))
      {
        $list['fans']=1;
      }
      else
      {
        $list['fans']=0;
      }
      //判断是不是我发表地文章
      if($openid==$uid)
      {
        $list['fans']=2;
      }
      //查询我又没有点赞这篇文章
      $dian=db('message')->where('mid',$mid)->field('id')->find();
      $arr1=array_filter(explode(',',$dian['id']));
      if(in_array($openid,$arr1))
      {
        $list['dian']=1;
      }
      else
      {
        $list['dian']=0;
      }
      //计算点赞数量
      $num=array();
      $num=array_filter(explode(',',$list['id']));
      $list['id']=count($num);
      //计算评论数量
      $cha=db('evalute')->where('message_id',$mid)->where('bpid',0)->select();
      $list['num2']=count($cha);
      //点赞的人
      if($list['id']!=0)
      {
        for($i=0;$i<count($num);$i++)
        {
          $diannum[]=db('user')->where('uid',$num[$i])->field('uid,uname,nickname')->find();
          $j=db('real')->where('uid',$num[$i])->find();
          if($j['rflg'] != 1)
          {
            $diannum[$i]['uname']=$diannum[$i]['nickname'];
          }
        }
      }
      else
      {
        //like点赞是不是为空
        $diannum=array();
      }
      $list['like']=$diannum;
      //浏览加一
      $list['num']=$list['num']+1;
      $jia=db('message')->where('mid',$mid)->update(['num'=>$list['num']]);
      //判断是否认证&是否是官方账号
      $a=db('real')->join('f_user us','us.uid=f_real.uid')->where('f_real.uid',$openid)->field('f_real.rflg,us.gflg')->find();
      //查询后台设置的转发次数限制
      $shezhi=db('shezhi')->where('zid',1)->field('userwenzhang,vipwenzhang,userbohao,vipbohao,userzhuanfa,vipzhuanfa')->find();
      $timestart=date("Y-m-d 00:00:00");
      $timeend=date("Y-m-d H:i:s");
      $shu=db('zhuanfa')
        ->where('zfid',$openid)
        ->where('f_zhuanfa.z_createtime','between',[$timestart,$timeend])
        ->count();//今日拨号
      if($data['phone']==''){
        $list['code'] = -2;
      }
      //官方账号无限制
      $list['myuid'] = $data['uid'];
      if($a['gflg']==1)
      {
        $list['shu']=$shu;
        $list['zhuan']=99999;
        return $this->AjaxReturn("1","操作成功",$list);
      }
      else
      {
        if($a['rflg']==1)
        {
          $list['shu']=$shu;
          $list['zhuan']=$shezhi['vipzhuanfa'];
          $list['a']=$a['rflg'];
          $a=Db::name('real')->where('uid',$openid)->field('tid')->find();
          $list['tid']=$a['tid'];
          return $this->AjaxReturn("1","操作成功",$list);
        }
        else
        {
          $list['shu']=$shu;
          $list['zhuan']=$shezhi['userzhuanfa'];
          $list['a']=$a['rflg'];
          $a=Db::name('real')->where('uid',$openid)->field('tid')->find();
          $list['tid']=$a['tid'];
          return $this->AjaxReturn("1","操作成功",$list);
        }
      }
    }
    else
    {
      return $this->AjaxReturn("-1","未接收到文章mid");
    }
  }

  //转载文章到自己的商圈(小程序里的转载)
  public function share(){
    $openid = input('openid');
    $myuid = db('user')->where('code',$openid)->value('uid');
    $mid = input('mid');
    //当前时间
    $time=date('Y-m-d H:i:s');
    //文章的信息
    $message=db('message')->where('mid',$mid)->find();
    //文章发布人的uid
    $uid=db('message')->where('mid',$mid)->value('uid');
    //添加数据
    $bool=db('message')->insert([
      'type' => 2,
      'picture' => $message['picture'],
      'title' => $message['title'],
      'content' => $message['content'],
      'createtime' => $time,
      'mflg' => 1,
      'uid' => $myuid,
      'state' => 0,
      'stoptime' => $message['stoptime'],
      'faxian' => 0,
      'usered' => $uid,
      'mided' => $message['mid'],
      'id' => $message['id'],
      'num' => $message['num']
    ]);
    if($bool){
      return $this->AjaxReturn('1','分享成功');
    }else{
      return $this->AjaxReturn('-1','分享失败');
    }
  }
  //产品信息的删除(测试)
  public function del()
  {
    $code=input('openid');
    $luid=Db::name('user')->where('code',$code)->field('uid')->find();
    $uid=$luid['uid'];
    $mid=input('delmid');
    $del=Db::name('message')
      ->where('f_message.mid',$mid)
      ->update([
        'mflg'=>0
      ]);
    if($del){
      return $this->AjaxReturn('1','删除成功');
    }else{
      return $this->AjaxReturn('-1','信息错误');
    }
  }
}